package io.renren.modules.sys.service.impl;

import io.renren.common.utils.Constant;
import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import com.baomidou.mybatisplus.core.conditions.Wrapper;

import io.renren.modules.sys.dao.GoodshelvesDao;
import io.renren.modules.sys.service.GoodshelvesService;


@Service("goodshelvesService")
public class GoodshelvesServiceImpl extends ServiceImpl<GoodshelvesDao, GoodshelvesEntity> implements GoodshelvesService {

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private SysRoleService sysRoleService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();

        Long roleId = sysUserRoleDao.queryRoleId(user.getUserId());
        String name = (String)params.get("name");
        String type = (String)params.get("type");
        String statu = (String)params.get("statu");

        String franchisee = "";
        String shop = "";
        if(roleId!=null) {
            SysRoleEntity role = sysRoleService.getById(roleId);
            if (role.getRoleName().indexOf("管理员") != -1) {  //当前登陆用户为平台

            }
            if (role.getRoleName().indexOf("加盟商") != -1 || role.getRoleName().indexOf("渠道") != -1) {//当前登陆用户为渠道(加盟商)
                franchisee = user.getUserId().toString();
            }
            if (role.getRoleName().indexOf("店铺") != -1) {//当前登陆用户为店铺
                shop = user.getUserId().toString();

            }
        }

        IPage<GoodshelvesEntity> page = this.page(
                new Query<GoodshelvesEntity>().getPage(params),
                new QueryWrapper<GoodshelvesEntity>().like(StringUtils.isNotBlank(name),"shelvesName",name)
                        .eq(StringUtils.isNotBlank(type),"type",type)
                        .eq(StringUtils.isNotBlank(statu),"statu",statu)
                        .eq(StringUtils.isNotBlank(franchisee),"franchiseeId",franchisee)
                        .eq(StringUtils.isNotBlank(shop),"shopId",shop)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );


        return new PageUtils(page);
    }

    @Override
    public List<ShelvesAnalysisEntity> getShelvesAnalysis(Map<String, Object> params){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Object startimeObj = params.get("startime");

        Object endtimeObj = params.get("endtime");
        String startime = "";
        String endtime = "";
        if(startimeObj !=null && endtimeObj !=null&&!startimeObj.equals("")&&!endtimeObj.equals("")){
            startime = startimeObj.toString();
            endtime = endtimeObj.toString();
        }else{
            endtime = sdf.format(new Date());
            Date  currdate = new Date();
            Calendar ca = Calendar.getInstance();
            ca.setTime(currdate);
            ca.add(Calendar.DATE, -6);
            currdate = ca.getTime();
            startime = sdf.format(currdate);
        }
        List<ShelvesAnalysisEntity> shelvesAnalysisEntities = baseMapper.getShelvesAnalysis(startime,endtime);
        return shelvesAnalysisEntities;
    }

    @Override
    public ShelvesAnalysisStatisticsEntity getStatistics(Map<String, Object> params){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Object startimeObj = params.get("startime");

        Object endtimeObj = params.get("endtime");
        String startime = "";
        String endtime = "";
        if(startimeObj !=null && endtimeObj !=null&&!startimeObj.equals("")&&!endtimeObj.equals("")){
            startime = startimeObj.toString();
            endtime = endtimeObj.toString();
        }else{
            endtime = sdf.format(new Date());
            Date  currdate = new Date();
            Calendar ca = Calendar.getInstance();
            ca.setTime(currdate);
            ca.add(Calendar.DATE, -6);
            currdate = ca.getTime();
            startime = sdf.format(currdate);
        }
        ShelvesAnalysisStatisticsEntity entity = baseMapper.getStatistics(startime,endtime);
        return entity;
    }

    @Override
    public List<SysUserEntity> getFranchisee(){
        List<SysUserEntity> sysUserEntityList = baseMapper.getFranchisee();
        return sysUserEntityList;
    }


    @Override
    public PageUtils querylistByParams(Map<String, Object> params){

        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数

        String sname = null;
        if( params.get("sname")!=null&&!params.get("sname").equals("")){
            sname = params.get("sname").toString();
        }

        String groupid = null;
        if( params.get("groupid")!=null&&!params.get("groupid").equals("")){
            groupid = params.get("groupid").toString();
        }
        int count = baseMapper.queryListcount(sname,groupid);
        List<GoodshelvesEntity> list = baseMapper.querylistByParams(sname,groupid,(currPage-1)*pageSize,currPage*pageSize);

        return new PageUtils(list,count,pageSize,currPage);
    }



}
